package com.szq.netty.client;

import com.szq.netty.future.SyncWriteFuture;
import com.szq.netty.future.SyncWriteMap;
import com.szq.netty.msg.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @ClassName: MyClientHandler
 * @Description:
 * @Author: szq
 * @Date: 2021-11-17 09:25:34
 */
public class MyClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Response response = (Response) msg;
        String requestId = response.getRequestId();
        SyncWriteFuture syncWriteFuture = (SyncWriteFuture) SyncWriteMap.syncKey.get(requestId);
        if (syncWriteFuture != null){
            syncWriteFuture.setResponse(response);
        }
        super.channelRead(ctx, msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
